package agentsSME;

import java.util.LinkedList;
import java.util.Random;

public class Stock {
	
	private double stockLevel; // stock units
	private double stockAboveGround;
	private double stockGround;
	private double ratioLocation; // ground/total
	private double ratioFragile;
	private Random random;
	private LinkedList<Products> storedProducts;
	
	public Stock(double value, double ratioLocation, double ratio){
		this.stockLevel = value;
		this.setRatioLocation(ratioLocation);
		this.setStockUnitsGround(this.stockLevel*ratioLocation);
		this.setStockUnitsAboveGround(this.stockLevel*(1.0-ratioLocation));
		ratioFragile=ratio;
		storedProducts = new LinkedList<Products>();
		random = new Random();
	}
	public void setStockLevel(double v){
		this.stockLevel = v;
		
	}
	//reduce r percent of current stock level
	public void useStock(double r){
		if(this.getStockLevel()>=r){
			setStockLevel(getStockLevel()-r);
			if(random.nextInt(2)==0 )
				if(getStockUnitsGround()>=r){
					double x=getStockUnitsGround()-r;					
					setStockUnitsGround(x);
					
				} else{					
					double left=r-getStockUnitsGround();					
					setStockUnitsAboveGround(getStockUnitsAboveGround()-left);
					setStockUnitsGround(0.0);
			}else{ 
				if(getStockUnitsAboveGround()>=r){		
					setStockUnitsAboveGround(getStockUnitsAboveGround()-r);
				}else{					
					double left=r-getStockUnitsAboveGround();
					setStockUnitsGround(getStockUnitsGround()-left);
					setStockUnitsAboveGround(0.0);
				}
			}
		}
		if(this.getStockUnitsGround()<0.001){
			this.setStockUnitsGround(0.0);
		}
		if(this.getStockUnitsAboveGround()<0.001){
			this.setStockUnitsAboveGround(0.0);
		}
	}
	
	public double getStockLevel(){
		return this.stockLevel;
	}
	
	
	public double getStockUnitsAboveGround() {
		return stockAboveGround;
	}
	public void setStockUnitsAboveGround(double stockAboveGround) {
		this.stockAboveGround = stockAboveGround;
	}
	public double getStockUnitsGround() {
		return stockGround;
	}
	public void setStockUnitsGround(double stockGround) {
		this.stockGround = stockGround;
	}
	public double getRatioFragile() {
		return ratioFragile;
	}
	public void setRatioFragile(double ratioFragile) {
		this.ratioFragile = ratioFragile;
	}
	public double getRatioLocation() {
		return ratioLocation;
	}
	public void setRatioLocation(double ratioLocation) {
		this.ratioLocation = ratioLocation;
	}
    
}
